﻿using System.Linq;
using System.Windows;
using System.Windows.Data;
using FU.Capstones.IMS.Web.Common;
using FU.Capstones.IMS.Web.Common.InventoryServiceProxy.DataContracts;
using FU.Capstones.IMS.Web.Common.InventoryServiceProxy.EventArgs;
using FU.Capstones.IMS.Web.Silverlight.Models;
using FU.Capstones.IMS.Web.Silverlight.Popups;
using Microsoft.Practices.Prism.Commands;
using Navigation.Silverlight;

namespace FU.Capstones.IMS.Web.Silverlight.ViewModels
{
    public class WarehouseListViewModel : ViewModelBase
    {
        public WarehouseListViewModel()
        {
            Model = new WarehouseListModel();
            ((WarehouseListModel)Model).GetWarehouseCollectionCompleted += DoGetWarehouseCollectionCompleted;
            ((WarehouseListModel)Model).DeleteWarehouseCompleted += DoDeleteWarehouseCompleted;
            ((WarehouseListModel)Model).UndoDeleteWarehouseCompleted += UndoDeleteWarehouseCompleted;
            ServiceCallCounter = 0;
            ServiceCallCounter += ((WarehouseListModel)Model).GetWarehouseCollectionAsync(IsShowAll);
            Message = WebConstants.LoadingMessage;

            CanEditOrAdd = CurrentUser.CanEditWarehouse;
            CanAdd = CurrentUser.CanAddWarehouse;
        }
        
        #region Do Completed Methods

        void DoDeleteWarehouseCompleted(object sender, FeedbackCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                Message = e.Error.Message;
                SetMessageVisibility(false);
            }
            else
            {
                Message = e.Result.Message;
                SetMessageVisibility(e.Result.IsSuccessful);
                if (e.Result.IsSuccessful)
                {
                    ServiceCallCounter += ((WarehouseListModel)Model).GetWarehouseCollectionAsync(IsShowAll);
                }
            }
            ServiceCallCounter -= WebConstants.MagicNumer;
        }

        void DoGetWarehouseCollectionCompleted(object sender, CollectionFeedbackCompletedEventArgs<Warehouse> e)
        {
            if (e.Error != null)
            {
                Message = e.Error.Message;
                SetMessageVisibility(false);
            }
            else
            {
                Message = e.Result.Message;
                SetMessageVisibility(e.Result.IsSuccessful);
                if (e.Result.IsSuccessful)
                {
                    WarehouseCollection = new PagedCollectionView(e.Result.Data);
                }
            }
            ServiceCallCounter -= WebConstants.MagicNumer;
        }

        void UndoDeleteWarehouseCompleted(object sender, FeedbackCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                Message = e.Error.Message;
                SetMessageVisibility(false);
            }
            else
            {
                Message = e.Result.Message;
                SetMessageVisibility(e.Result.IsSuccessful);
                if (e.Result.IsSuccessful)
                {
                    NavigationManager.NavigateTo(WebConstants.NavigationPages.WarehouseDetail, new NavigationData() { NavigationUri = WebConstants.NavigationPages.WarehouseList, Data = currentWarehouseID });    
                }
            }
            ServiceCallCounter -= WebConstants.MagicNumer;
        }

        #endregion

        #region Commands

        #region ShowAllCommand

        private DelegateCommand<object> showAllCommand;
        public DelegateCommand<object> ShowAllCommand
        {
            get
            {
                if (showAllCommand == null)
                {
                    showAllCommand = new DelegateCommand<object>(DoShowAllCommand);
                }
                return showAllCommand;
            }
        }

        private void DoShowAllCommand(object obj)
        {
            ServiceCallCounter +=
                ((WarehouseListModel)Model).GetWarehouseCollectionAsync(IsShowAll);
        }

        #endregion

        #region EditCommand

        private DelegateCommand<int?> editCommand;
        public DelegateCommand<int?> EditCommand
        {
            get
            {
                if (editCommand == null)
                {
                    editCommand = new DelegateCommand<int?>(DoEditCommand);
                }
                return editCommand;
            }
        }

        private void DoEditCommand(int? obj)
        {
            if (obj.HasValue)
            {
                currentWarehouseID = obj.Value;
                Warehouse w = WarehouseCollection.Cast<Warehouse>().FirstOrDefault(x => x.WarehouseID == currentWarehouseID);
                if (w.IsDelete)
                {
                    UndoDeleteConfirmationPopup popup = new UndoDeleteConfirmationPopup();
                    popup.Show();
                    popup.Closed += (sender, e) =>
                                        {
                                            if (popup.DialogResult.HasValue && popup.DialogResult.Value)
                                            {
                                                ServiceCallCounter += ((WarehouseListModel) Model).UndoDeleteWarehouseAsync(currentWarehouseID);
                                            }
                                        };
                }
                else
                {
                    NavigationManager.NavigateTo(WebConstants.NavigationPages.WarehouseDetail, new NavigationData() { NavigationUri = WebConstants.NavigationPages.WarehouseList, Data = currentWarehouseID });    
                }
            }
        }

        #endregion

        #region DeleteCommand

        private DelegateCommand<int?> deleteCommand;
        public DelegateCommand<int?> DeleteCommand
        {
            get
            {
                if (deleteCommand == null)
                {
                    deleteCommand = new DelegateCommand<int?>(DoDeleteCommand);
                }
                return deleteCommand;
            }
        }

        private void DoDeleteCommand(int? obj)
        {
            if (obj.HasValue)
            {
                DeleteConfirmationPopup popup = new DeleteConfirmationPopup();
                popup.Show();
                popup.Closed += (sender, e) =>
                {
                    if (popup.DialogResult.HasValue && popup.DialogResult.Value)
                    {
                        ServiceCallCounter +=
                            ((WarehouseListModel)Model).DeleteWarehouseAsync(obj.Value);
                        currentWarehouseID = obj.Value;
                        Message = "Deleting the selected Warehouse!";
                    }
                };
            }
        }

        #endregion

        #region CreateCommand

        private DelegateCommand<Warehouse> createCommand;
        public DelegateCommand<Warehouse> CreateCommand
        {
            get
            {
                if (createCommand == null)
                {
                    createCommand = new DelegateCommand<Warehouse>(DoCreateCommand);
                }
                return createCommand;
            }
        }

        private void DoCreateCommand(object obj)
        {
            NavigationManager.NavigateTo(WebConstants.NavigationPages.WarehouseDetail, new NavigationData(){NavigationUri = WebConstants.NavigationPages.WarehouseList, Data = 0});
        }

        #endregion

        #endregion

        #region Binding Properties

        private PagedCollectionView warehouseCollection;
        public PagedCollectionView WarehouseCollection
        {
            get { return warehouseCollection; }
            set
            {
                if (!ReferenceEquals(warehouseCollection, value))
                {
                    warehouseCollection = value;
                    RaisePropertyChanged(() => WarehouseCollection);
                }
            }
        }

        #endregion

        #region Fields

        private int currentWarehouseID;

        #endregion

        #region Implementation of INavigationAware

        public override void OnNavigate(object extraData)
        {
            //((EmployeeListModel) model).GetEmployeeCollectionAsync();
        }

        public override void OnLeave()
        {
            //throw new NotImplementedException();
        }

        #endregion
    }
}
